/**
 * A TaLib to make use of prototype-window javascript libs
 */


class PwTagLib {

    static namespace = "pw"

    def lib = {
        def baseUri = grailsAttributes.getApplicationUri(request)
        out << """
<script src="$baseUri/js/prototype/prototype.js" type="text/javascript">
</script>
<script src="$baseUri/js/pw/effects.js" type="text/javascript">
</script>
<script src="$baseUri/js/pw/window.js" type="text/javascript">
</script>
<script src="$baseUri/js/pw/window_effects.js" type="text/javascript">
</script>
<link href="$baseUri/css/pw/default.css" rel="stylesheet">
</link>
<link href="$baseUri/css/pw/alphacube.css" rel="stylesheet">
"""
    }

    /**
     * Display a button that when user click it a modal popup window will display.
     * usage:
     * <pw:popButton submitFunction="function(win){alert('ok');Windows.focusedWindow.close();}"
              submitLabel="提交" cancelLabel="取消" windowWidth="300" theme="alphacube"
              buttonLabel="添加">
        <p>
          <span class="login_label">金额</span>
          <span class="login_input"><input type="text"/></span>
        </p>
        <div style="clear:both"></div>
        <p>
          <span class="login_label">描述</span>
          <span class="login_input"><input type="text"/></span>
        </p>
        <div style="clear:both"></div>

      </pw:popButton>
     *
     * mandatory attributes:buttonLabel 
     */

    def popButton = {attr, body ->
        def contentId = new Date().getTime()
        def type = attr['type']?attr['type']:'confirm'
        def submitFunction = attr['submitFunction']?attr['submitFunction']:'function(win){win.close();}'
        def submitLabel = attr['submitLabel']?attr['submitLabel']:'Submit'
        def cancelLabel = attr['cancelLabel']?attr['cancelLabel']:'Cancel'
        def windowWidth = attr['windowWidth']?attr['windowWidth']:400
        def windowHeight = attr['windowHeight']?attr['windowHeight']:300
        def theme = attr['theme']?attr['theme']:'alphacube'
        def buttonLabel = attr['buttonLabel']

        out << """
      <script type="text/javascript">
        function open_${contentId}() {
          Dialog.${type}(\$('${contentId}').innerHTML,
          {className:"${theme}",
            width:${windowWidth},
            height:${windowHeight},
            okLabel: "${submitLabel}",
            cancelLabel: "${cancelLabel}",
            onOk:${submitFunction}
          }
                  );
        }
      </script>
      <span class="button">
          <input id="open_${contentId}_click_button" type="button" class="create"
                  onclick="open_${contentId}();" value="${buttonLabel}"
                  ></input>
      </span>
      <div id="${contentId}" style="display:none">
        """
        out << body()
        out << "</div>"

    }

}
